﻿using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using DeGiglio.biz.Twitter.Classes;
using Microsoft.SharePoint.Client.Services;

namespace DeGiglio.biz.Twitter.Services
{
    [BasicHttpBindingServiceMetadataExchangeEndpoint]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceContract]
    public class TwitterServices
    {
        /// <summary>
        /// The LoadTweets web method is used to return a collection of tweet objects for either a specific user or a user's curated list.
        /// The method does not contain explicit error handling as we're relying on the custom error handler contained in the TwitterServicesCustomErrorHandler.cs file
        /// </summary>
        /// <param name="screenName">The Twitter screen name for which you want to retrieve tweets</param>
        /// <param name="listName">Optional - the curated list of tweets you wish to retrieve</param>
        /// <param name="requestedItems">How many tweets to retrieve?</param>
        /// <param name="cacheWindow">Cache time in minutes for which the server should wait before requesting tweets from the API</param>
        /// <param name="includeRetweets">Boolean indicating if retweets should be included in the return list</param>
        /// <param name="loadAvatarsUsingSsl">Boolean indicating if avatars should be loaded using SSL (This is useful if the host page is also loaded using SSL)</param>
        /// <param name="conKey">The OAuth consumer key</param>
        /// <param name="conSec">The OAuth consumer secret</param>
        /// <param name="localeId">The locale ID for the site or user</param>
        /// <returns>A list of tweets according the parameters specified</returns>
        [OperationContract]
        [WebInvoke(Method = "POST",
         BodyStyle = WebMessageBodyStyle.Wrapped,
         ResponseFormat = WebMessageFormat.Json)]
        public List<Tweet> LoadTweets(string screenName, string listName, int requestedItems, int cacheWindow,
                                      bool includeRetweets, bool loadAvatarsUsingSsl, 
                                      string conKey, string conSec, int localeId)
        {
            TwitterHelper helper = null;

            // Determine which constructor to use based on wanting just user tweets or a list
            if (string.IsNullOrEmpty(listName))
            {
                // We're asking for a user's tweets
                helper = new TwitterHelper(screenName, requestedItems, includeRetweets);
            }
            else
            {
                // We're asking for a curated list
                helper = new TwitterHelper(listName, screenName, requestedItems, includeRetweets);
            }

            // If any of the authentication parameters were not set, 
            // tell the helper that we're in the "not set" state. This will ultimately throw an error, but
            // it's better to have that logic in the helper class
            if ((string.IsNullOrEmpty(conKey)) ||
                (string.IsNullOrEmpty(conSec)))
            {
                helper.AuthenticateAs = TwitterHelper.AuthenticationType.NotSet;
            }
            else
            {
                helper.AuthenticateAs = TwitterHelper.AuthenticationType.Authenticated;
                helper.Twitter_ConsumerKey = conKey;
                helper.Twitter_ConsumerSecret = conSec;
            }

            helper.CacheTimespan = cacheWindow;
            helper.UICultureLocaleId = localeId;

            List<Tweet> tweetCollection = helper.GetTweets(loadAvatarsUsingSsl);

            // Update the "display time" property to reflect how long it's been since the tweet was created
            foreach (Tweet tweet in tweetCollection)
            {
                tweet.DisplayTime = helper.DetermineDisplayDiff(tweet.CreatedAt);
            }

            return tweetCollection;
        }
    }
}
